package com.primaryedu.ui.center;

import java.io.File;
import java.io.IOException;

import org.json.JSONException;

import com.primaryedu.R;
import com.primaryedu.dialog.CustomDialog;
import com.primaryedu.loadimage.ImageFileCache;
import com.primaryedu.ui.BaseAppCompatActivity;
import com.primaryedu.ui.app.nicedialog.BaseNiceDialog;
import com.primaryedu.ui.app.nicedialog.DialogCallBackUtils;
import com.primaryedu.ui.app.nicedialog.DialogSureCallBack;
import com.primaryedu.ui.app.nicedialog.MyCustomDialog;
import com.primaryedu.utils.DataCleanManager;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.TextView;

/**
 * @author jun.wang
 * @description 清除缓存
 * @data 2016年5月16日
 */
public class ClearChcheActivity extends BaseAppCompatActivity implements OnClickListener, DialogSureCallBack {
    private CustomDialog.Builder ibuilder;
    private TextView text_clear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_clear_chche);

    }


    @Override
    protected int BindLayout() {
        return R.layout.activity_clear_chche;
    }

    @Override
    protected void initView() {
        DialogCallBackUtils.setCallBack(this);
        ((TextView) findViewById(R.id.title)).setText("缓存管理");
        findViewById(R.id.back).setOnClickListener(this);
        text_clear = (TextView) this.findViewById(R.id.text_clear);
        text_clear.setText(DataCleanManager.getFormatSize(ImageFileCache.calculateImageSize()) + "");
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
//            case R.id.text_clear:
//                show();
//                break;

            case R.id.back:
                finish();
                break;

            default:
                break;
        }

    }

    private void show() {
        ibuilder = new CustomDialog.Builder(ClearChcheActivity.this);
        ibuilder.setTitle("清除缓存");
        ibuilder.setMessage("清除缓存会导致下载的内容删除，是否确定?");
        ibuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        ibuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                ImageFileCache.ClearCache();
                showToast("缓存已清除");
                text_clear.setText("0.0B");
                dialog.dismiss();
            }
        });

        Dialog dialog = ibuilder.create();
        dialog.show();
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay();  //为获取屏幕宽、高
        android.view.WindowManager.LayoutParams p = dialog.getWindow().getAttributes();  //获取对话框当前的参数值
        p.width = (int) (d.getWidth() * 0.8);    //宽度设置为屏幕的0.9
        dialog.getWindow().setAttributes(p);     //设置生效
    }

    public void showdialogclearchche(View view){
        MyCustomDialog.newInstance("clearchche")
                .setMargin(60)
                .setOutCancel(false)
                .show(getSupportFragmentManager());
    }

    @Override
    public void SureClick(String type, BaseNiceDialog dialog) {
        if ("clearchche".equals(type)){
            ImageFileCache.ClearCache();
            showToast("缓存已清除");
            text_clear.setText("0.0B");

        }
        dialog.dismiss();
    }
}
